package com.shiliao.app.activity.index.ui;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.shiliao.app.R;
import com.shiliao.app.activity.index.entity.AddRemindEntity;
import com.shiliao.app.activity.index.presenter.AddRemindPresenter;
import com.shiliao.app.activity.index.view.AddRemindView;
import com.shiliao.app.manager.YMUserManager;
import com.shiliao.app.weight.ActionBarView;
import com.yimei.framework.base.ui.BaseActivity;
import com.yimei.framework.ui.recyclerview.CommonAdapter;
import com.yimei.framework.ui.recyclerview.base.ViewHolder;
import com.yimei.framework.util.LogUtil;

import java.util.List;

import butterknife.BindView;

/**
 * Created by yinzhiqiang on 2017/10/9.
 */

public class AddRemindActivity extends BaseActivity implements AddRemindView {

    @BindView(R.id.title_bar)
    ActionBarView titleBar;

    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;

    private AddRemindPresenter addRemindPresenter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_add_remind;
    }

    @Override
    public void initView() {
        super.initView();
        titleBar.setTitles("添加日常提醒");
        titleBar.setBackClick(this);
        titleBar.getBackView().setVisibility(View.VISIBLE);
        addRemindPresenter = new AddRemindPresenter(this);
        Intent intent = getIntent();
        String article_id = intent.getStringExtra("article_id");
        LogUtil.e("yzq", article_id);
        addRemindPresenter.requestRemind(article_id);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
    }


    @Override
    public void setAdapter(final List listData) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (listData != null) {
                    recyclerView.setAdapter(new CommonAdapter<AddRemindEntity>(getBaseContext(), R.layout.item_remind, listData) {
                        @Override
                        protected void convert(ViewHolder holder, final AddRemindEntity entity, int position) {
                            LogUtil.e("yzq", listData.size() + "");
                            holder.setText(R.id.add_remind, "添加提醒");
                            holder.setText(R.id.tv_remind_time,"提醒频率:"+entity.frequency+"天一次");
                            if (!TextUtils.isEmpty(entity.content)) {
                                holder.setText(R.id.tv_conttent, entity.content);
                            }
                            holder.setOnClickListener(R.id.add_remind, new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    String userId = YMUserManager.getUserId(AddRemindActivity.this);
                                    addRemindPresenter.requestAddRemind(entity.reminder_id, userId);
                                    showLoadingView();
                                }
                            });

                        }

                    });
                }
            }
        });
    }

    @Override
    public void showToast() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast("添加成功");
                hideLoadingView();
            }
        });
    }
}
